//
//  HomeCollectionCell.swift
//  我的微博
//
//  Created by shmily on 15/8/2.
//  Copyright © 2015年 Stanford-IOS8-Swift-shmilyAshen. All rights reserved.
//

import UIKit

/// 可重用 ID
enum HomeCellID: String {
    case NormalCell = "NormalCellID"
    case RetweetedCell = "retweetedCellID"
    
    static func cellID(status: BlogContent) ->String {
        return (status.retweeted_status == nil) ? NormalCell.rawValue : RetweetedCell.rawValue
    }
}

/// home控制器的 cell
class HomeTableViewCell: UITableViewCell {
    /// 可重用 ID
   private let homeTableViewCellID = "homeTableViewCellID"
    /// cell 需要显示的微博数据 
    var status: BlogContent? {
        didSet{
            topView.status = status
            pictureView.status = status
            lblContent.text = status?.text ?? ""
            
            picWidthCons?.constant = pictureView.bounds.width
            picHeightCons?.constant = pictureView.bounds.height
            bottomYCons?.constant = (pictureView.bounds.height == 0) ? 0 : homeTopViewMargin
        }
    }
    
    /// pictureView 的宽度约束
    var picWidthCons: NSLayoutConstraint?
    /// pictureView 的高度约束
    var picHeightCons: NSLayoutConstraint?
    /// 底部视图的 Y 约束,当 pictureView 为0时,防止2倍间距
    var bottomYCons: NSLayoutConstraint?

    /// 计算行高
    func calRowHeight(status: BlogContent) ->CGFloat {
        self.status = status
        
        layoutIfNeeded()
        return CGRectGetMaxY(bottomView.frame)
    }
    
    /// 添加控件
    func addControl() {
        contentView.addSubview(topSeperatorView)
        contentView.addSubview(topView)
        contentView.addSubview(lblContent)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
//        //设置布局
        topSeperatorView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 10))
//        //顶部视图布局
        topView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topSeperatorView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53))
//        //文本视图
        lblContent.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil,offset: CGPoint(x: homeTopViewMargin, y: homeTopViewMargin))
        contentView.addConstraint(NSLayoutConstraint(item: lblContent, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: -CGFloat(2 * homeTopViewMargin)))
//        //图片视图,这里 size 设置任意值,为了设置约束后期修改
//       let cons = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: lblContent, size: CGSize(width: 290, height: 290), offset: CGPoint(x: homeTopViewMargin, y: homeTopViewMargin))
//        //获取 pictureView 的宽高约束
//        picWidthCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
//        picHeightCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
        
        //底部视图的布局
        let consBottom = bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44),offset: CGPoint(x: 0, y:homeTopViewMargin))
         bottomYCons = bottomView.ff_Constraint(consBottom, attribute: NSLayoutAttribute.Top)
    }
    
    //MARK:- 初始化视图
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: homeTableViewCellID)
        
        addControl()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:- 懒加载视图
    //分隔符视图
    private lazy var topSeperatorView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.lightGrayColor()
        return view
        }()
    //顶部视图
    private lazy var topView: HomeTopView = HomeTopView()
    //文本视图
    lazy var lblContent: UILabel = {
        let lbl = UILabel()
        lbl.numberOfLines = 0
        // 设置文本折行的宽度，如果不设置此属性，有的时候，行高不准确！
        lbl.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * CGFloat(homeTopViewMargin)
        return lbl
    }()
    //图像视图
    lazy var pictureView: HomePictureView = HomePictureView()

    //底部视图
    lazy var bottomView: HomeBottomView = HomeBottomView()
    
    
}
